package com.xuerhua.yxt.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.model.Attachment;
import com.xuerhua.yxt.model.AttachmentList;
import com.xuerhua.yxt.service.IAttachmentListService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.FileUtils;
import com.xuerhua.yxt.wechat.utils.FileUtil;

@Controller
public class AttachmentController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(AttachmentController.class);
	
	@Autowired
	private IAttachmentListService attachmentListService;

	@RequestMapping(Const.TEACHER_ROLE + "/attchment/upload/wx")
	@ResponseBody Object wxAttchmentUpload(Attachment attach) {
		try {
			AppUser appUser = getAppUser();
			String path = FileUtil.wxuploadImage(attach.getMediaId(), attach.getRelationType());
			//String path = FileUtils.writeLocalFile(reqVO.getBaseData(), reqVO.getExtName(), RelationType.NOTICE.getCode());
			if(path == null) {
				return new RspResult(Status.FAIL);
			}
			AttachmentList attachment = BeanUtils.copyAs(attach, AttachmentList.class);
			attachment.setTeacherId(appUser.getTeacher().getId());
			attachment.setSchoolId(appUser.getTeacher().getSchoolId());
			attachment.setAttachmentUrl(path);
			attachment.setAttachmentExtUrl(attach.getMediaId());
			attachmentListService.insertSelective(attachment);
			return new RspResult(Status.SUCCESS, attachment);
		} catch (Exception e) {
			logger.error("----wxAttchmentUpload error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.STUDENT_ROLE + "/attchment/upload/wx")
	@ResponseBody Object studentWxAttchmentUpload(Attachment attach) {
		try {
			AppUser appUser = getAppUser();
			String path = FileUtil.wxuploadImage(attach.getMediaId(), attach.getRelationType());
			//String path = FileUtils.writeLocalFile(reqVO.getBaseData(), reqVO.getExtName(), RelationType.NOTICE.getCode());
			if(path == null) {
				return new RspResult(Status.FAIL);
			}
			AttachmentList attachment = BeanUtils.copyAs(attach, AttachmentList.class);
			attachment.setAttachmentUrl(path);
			attachment.setAttachmentExtUrl(attach.getMediaId());
			attachment.setParentStudentRelId(appUser.getParent().getRelId());
			attachment.setSchoolId(appUser.getParent().getSchoolId());
			attachmentListService.insertSelective(attachment);
			return new RspResult(Status.SUCCESS, attachment);
		} catch (Exception e) {
			logger.error("----wxAttchmentUpload error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + "/attchment/upload/lrz")
	@ResponseBody Object lrzAttchmentUpload(Attachment attach) {
		try {
			AppUser appUser = getAppUser();
			String path = FileUtils.writeLocalFile(attach.getBaseData(), attach.getExtName(), attach.getRelationType());
			if(path == null) {
				return new RspResult(Status.FAIL);
			}
			AttachmentList attachment = BeanUtils.copyAs(attach, AttachmentList.class);
			attachment.setSchoolId(appUser.getTeacher().getSchoolId());
			attachment.setTeacherId(appUser.getTeacher().getId());
			attachment.setAttachmentUrl(path);
			attachmentListService.insertSelective(attachment);
			return new RspResult(Status.SUCCESS, attachment);
		} catch (Exception e) {
			logger.error("----lrzAttchmentUpload error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.STUDENT_ROLE + "/attchment/upload/lrz")
	@ResponseBody Object studentLrzAttchmentUpload(Attachment attach) {
		try {
			AppUser appUser = getAppUser();
			String path = FileUtils.writeLocalFile(attach.getBaseData(), attach.getExtName(), attach.getRelationType());
			if(path == null) {
				return new RspResult(Status.FAIL);
			}
			AttachmentList attachment = BeanUtils.copyAs(attach, AttachmentList.class);
			attachment.setAttachmentUrl(path);
			attachment.setParentStudentRelId(appUser.getParent().getRelId());
			attachmentListService.insertSelective(attachment);
			return new RspResult(Status.SUCCESS, attachment);
		} catch (Exception e) {
			logger.error("----lrzAttchmentUpload error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + "/attchment/upload/file")
	@ResponseBody Object lrzAttchmentUpload(Attachment attach, 
			@RequestParam(value="files") MultipartFile buildInfo) {
		try {
			AppUser appUser = getAppUser();
			String path = FileUtils.writeFile(attach.getExtName(), attach.getRelationType(), buildInfo);
			if(path == null) {
				return new RspResult(Status.FAIL);
			}
			AttachmentList attachment = BeanUtils.copyAs(attach, AttachmentList.class);
			attachment.setTeacherId(appUser.getTeacher().getId());
			attachment.setAttachmentUrl(path);
			attachment.setSchoolId(appUser.getTeacher().getSchoolId());
			attachmentListService.insertSelective(attachment);
			return new RspResult(Status.SUCCESS, attachment);
		} catch (Exception e) {
			logger.error("----lrzAttchmentUpload error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + "/attchment/handing")
	@ResponseBody Object attchmentHanding(Attachment attachment) {
		try {
			AppUser appUser = getAppUser();
			AttachmentList record = new AttachmentList();
			record.setTeacherId(appUser.getTeacher().getId());
			record.setRelationType(attachment.getRelationType());
			record.setRelationId(attachment.getRelationId());
			record.setSchoolId(appUser.getTeacher().getSchoolId());
			return attachmentListService.selectHandingList(record);
		} catch (Exception e) {
			logger.error("----attchmentHanding error={}", e);
		}
		return null;
	}
	
	@RequestMapping("attachment/del")
	@ResponseBody Object teacherAttachmentDel(Long attachId) {
		try {
			AttachmentList attach = new AttachmentList();
			attach.setId(attachId);
			attach.setIsDelete("Y");
			int count = attachmentListService.updateByPrimaryKeySelective(attach);
			if(count > 0) return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---teacherAttachmentDel error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
}
